package com.example.springbootasync.handler;

import com.example.springbootasync.config.AsyncConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import java.lang.reflect.Method;
import java.util.Arrays;

/***
 * 处理异步方法中未捕获的异常
 *
 * @author wliduo[i@dolyw.com]
 * @date 2020/5/19 19:16
 */
public  class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    /**
     * logger
     */
    private static final Logger logger = LoggerFactory.getLogger(AsyncConfig.class);
    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
        logger.info("Exception message - {}", throwable.getMessage());
        logger.info("Method name - {}", method.getName());
        logger.info("Parameter values - {}", Arrays.toString(obj));
        if (throwable instanceof Exception) {
            Exception exception = (Exception) throwable;
            logger.info("exception:{}", exception.getMessage());
        }
        throwable.printStackTrace();
    }
}
